package com.zw.fzbilibili.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zw.fzbilibili.dao.SysUserDao;
import com.zw.fzbilibili.entity.SysUser;
import com.zw.fzbilibili.enums.StatusEnum;
import com.zw.fzbilibili.service.AuthService;
import com.zw.fzbilibili.utils.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collections;

/**
 * <p>
 *
 * </p>
 *
 * @author zhangwen
 * @since 2022/12/23
 */
@Service
@Slf4j
public class AuthServiceImpl implements AuthService {

    @Resource
    private AuthenticationManager authenticationManager;

    @Resource
    private UserDetailsService userDetailsService;

    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @Resource
    private SysUserDao sysUserDao;

    @Override
    public Integer register(SysUser sysUser) {
        String username = sysUser.getUsername();
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, username); // 用户名
        queryWrapper.eq(SysUser::getStatus, StatusEnum.STATUS_0.getCode()); // 未删除状态
        if (CollectionUtil.isNotEmpty(sysUserDao.selectList(queryWrapper))) {
            return null;
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); // 对用户密码进行加密
        String rawPassword = sysUser.getPassword();
        sysUser.setPassword(encoder.encode(rawPassword));

        // 设置默认值
        sysUser.setCreated(LocalDateTime.now());
        sysUser.setUpdated(LocalDateTime.now());
        sysUser.setStatus(StatusEnum.STATUS_0.getCode());
        return sysUserDao.insert(sysUser);
    }

    @Override
    public String login(String username, String password) {
        UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(username, password);
        try {
            Authentication authentication = authenticationManager.authenticate(upToken);
            SecurityContextHolder.getContext().setAuthentication(authentication);

            UserDetails userDetails = userDetailsService.loadUserByUsername(username);
            String token = jwtTokenUtil.generateToken(userDetails);
            return token;
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return "账户名或者密码有误！！！";
    }
}
